package org.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.example.entity.Asset;
import org.example.mapper.AssetMapper;
import org.example.service.AssetService;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author κ��
 * @description ��Ա�tb_asset�������ݿ����Serviceʵ��
 * @createDate 2024-01-05 18:41:41
 */
@Slf4j
@Service
public class AssetServiceImpl extends ServiceImpl<AssetMapper, Asset>
        implements AssetService {
    @Override
    public boolean insertAsset(Asset asset) {
        Date date = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        asset.setAssetNo(dateFormat.format(date) + "_" + getSerialNumber());//�Զ�����assetNo
        asset.setRegisterTime(date);//���ô���ʱ��
        asset.setLastUpdatedTime(date);//��������޸�����
        return baseMapper.insertAsset(asset);
    }

    @Override
    public boolean updateAsset(Asset asset) {
        asset.setLastUpdatedTime(new Date());
        return baseMapper.updateAsset(asset);
    }

    @Override
    public boolean deleteAsset(int id) {
        return baseMapper.deleteAsset(id);
    }
    public Asset getAssetById(int id){
        return baseMapper.getAssetById(id);
    }

    @Override
    public List<Asset> getAssetList(String name, String category, String label, int orgId) {
        if (orgId != -1) {
            List<Map<String, Integer>> maps = baseMapper.getOrgListWithFather();
            int[] ids = new int[maps.size()];//�洢��ͨ��id
            int[] delete = new int[maps.size()];//ɾ��id
            int end = 1;
            int insert = 0;
            int delete_len = 0;
            ids[0] = orgId;
            Map<String, Integer> temp;
            while (end != insert) {
                int father = ids[insert];
                insert++;
                for (int i = 0; i < maps.size(); i++) {
                    temp = maps.get(i);
                    if (temp.containsKey("fatherOrg") && temp.get("fatherOrg") == father) {
                        ids[end++] = temp.get("id");
                        delete[delete_len++] = i;
                    }
                }
                while (delete_len > 0)
                    maps.remove(delete[--delete_len]);
            }
            ids = Arrays.copyOfRange(ids, 0, end);
            log.info("pass org id:"+Arrays.toString(ids));
            return baseMapper.getAssetList(name, category, label, ids);
        }
        return baseMapper.getAssetList(name, category, label, null);
    }

    @Override
    public List<Map<String, Integer>> getOrgList() {
        return baseMapper.getOrgList();
    }

    private int getSerialNumber() {//��ȡ���յ���󴴽�
        String assetNo = baseMapper.getMaxAssetNo();
        if (assetNo == null) return 100;
        String no = assetNo.substring(assetNo.length() - 3);
        System.out.println(no);
        return Integer.parseInt(no) + 1;
    }
}




